package net.common.manager;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.naire.modul.code_inc.service.Code_incEditorService;
import com.naire.modul.code_inc.service.Code_incFindService;
import com.naire.modul.code_inc.vo.Code_incVo;
import com.naire.modul.sys_code.service.Sys_codeSearchService;
import com.naire.modul.sys_code.vo.Sys_codeVo;

/**
 * 系统编码管理
 * 
 * @author Administrator
 *
 */
public class SystemCodeManager {
	
	private static Map<String, Sys_codeVo> CODE = new HashMap<String, Sys_codeVo>();
	private static String SPACE = "000000000000000000";
	
	public static void init() throws Exception {
		Sys_codeSearchService service = new Sys_codeSearchService();
		service.invoke();
		for(Sys_codeVo v : service.getList()) {
			CODE.put(v.getCode_num(), v);
		}
	}
	
	public static Sys_codeVo get(String key) {
		return CODE.get(key);
	}
	
	public static void set(Sys_codeVo v) {
		CODE.put(v.getCode_num(), v);
	}
	
	public static Collection<Sys_codeVo> getList() {
		return CODE.values();
	}
	
	/**
	 * 系统编码
	 * 
	 * @param key
	 * @return
	 * @throws Exception 
	 */
	public static String createCode(String key) throws Exception {
		return createCode(key, null);
	}
	
	/**
	 * 编码长度
	 * 
	 * @param key
	 * @param preffix
	 * @return
	 * @throws Exception
	 */
	public static String createCode(String key, String preffix) throws Exception {
		return createCode(key, preffix, null);
	}
	
	/**
	 * 系统编码规则
	 * 
	 * @param key
	 * @param preffix
	 * @return
	 * @throws Exception 
	 */
	public static String createCode(String key, String preffix, String syspreffix) throws Exception {
		String code = null;
		synchronized (CODE) {
			Sys_codeVo v = get(key);
			String type = v.getCode_type();
			if(type.equals("0001")) {
				// 有层级
				int len = 0;
				String[] level = v.getStyle().split(","); // 各个层级长度
				if(preffix == null || preffix.equals("")) {
					len = Integer.parseInt(level[0]);
				} else {
					int i = getLevel(level, preffix);
					len = Integer.parseInt(level[i]);
				}
				String suffix = getCode(v.getCode_num(), preffix);
				if(preffix != null && !preffix.equals("")) {					
					code = preffix + SPACE.substring(0, len - suffix.length()) + suffix;
				} else {
					code = SPACE.substring(0, len - suffix.length()) + suffix;
				}
			} else if(type.equals("0002")) {
				// 无层级
				int len = v.getCode_len(); // 编码长度
				int preffixLen = 0; // 前缀长度
				if(v.getPreffix() != null) {
					preffixLen = v.getPreffix().length();
				}
				String suffix = getCode(v.getCode_num(), v.getPreffix());
				if(v.getPreffix() != null) {					
					code =  v.getPreffix() + SPACE.substring(0, len - preffixLen - suffix.length()) + suffix;
				} else {
					code = SPACE.substring(0, len - preffixLen - suffix.length()) + suffix;
				}
				if(code.length() > len) {
					throw new Exception("the code is too larger");
				}
			}
		}
		return code;
	}
	
	private static String getCode(String code_num, String preffix) throws Exception {
		int l = 1;
		Code_incFindService service = new Code_incFindService();
		service.setCode_num(code_num);
		service.setPreffix(preffix);
		service.execute();
		Code_incVo bean = service.getBean();
		if(bean != null) {
			l = bean.getInc();
			bean.setInc(bean.getInc() + 1);
		} else {
			bean = new Code_incVo();
			bean.setInc(2);
			bean.setCode_num(code_num);
			if(preffix != null) {				
				bean.setPreffix(preffix);
			}
		}
		updateInc(bean);
		return String.valueOf(l);
	}
	
	private static void updateInc(Code_incVo bean) throws Exception {
		Code_incEditorService service = new Code_incEditorService();
		service.setBean(bean);
		service.execute();
	}
	
	private static int getLevel(String[] level, String preffix) throws Exception {
		int l = 0;
		int j = 0;
		for(String s : level) {
			j++;
			l += Integer.parseInt(s);
			if(preffix.length() == l) {
				break;
			}
		}
		if(j >= level.length) {
			throw new Exception("indexof -1");
		}
		return j;
	}
	
}
